<?php
// +----------------------------------------------------------------------
// |  帐户控制器
// +----------------------------------------------------------------------
namespace app\admin\controller\soft;
use think\Controller;
use think\facade\Cookie;
use auth\Auths;

class Auth extends Controller
{
	// 默认的设置参数
	private $name = 'xw_system_user';   					// 用户表名-全称
	private $key = 'user_guid';		 							// 用户表名-全称
	private $login_code_field = 'user_code'; 				// 登录账号字段
	private $login_pwd_field = 'user_pwd';				// 登录密码字段
	private $signName = 'admin_user_auth_sign';		// 签名名称
	
	/**
     * 登录
	 * @access public
     * @param string $user_name   	用户名称
     * @param string $user_pwd 		用户密码
     * @return json
	 * @is_need_login false
    */
    public function login($user_name,$user_pwd)
    {
    	if( empty($user_name) )
			return json(array("code"=>-1,"data"=>null,"msg"=>"用户名不能为空"));
		if( empty($user_pwd) )
			return json(array("code"=>-1,"data"=>null,"msg"=>"密码不能为空"));
		
		//长度大于10为店长登录
		if( mb_strlen($user_name)==11 ){
			$vipInfo = \Db::table('xw_user_vip')->alias('a')
				->leftJoin("xw_shop b",'b.uid = a.uid')
				->where('a.phone' ,$user_name)
				->where('a.password', md5($user_pwd) )
				->whereNotNull('b.id')
				->field('a.*,b.id as shop_id,b.name as shop_name,b.is_enable')
				->find();
				
			if( empty($vipInfo) == true ){
				return json(array("code"=>-1,"msg"=>"账号和密码不符"));
			}
			if( $vipInfo['is_enable'] == "0" ){
				return json(array("code"=>-1,"msg"=>"该账户已被禁用"));
			}	
			$res = \Db::table($this->name)->where( 'user_code', $vipInfo['phone'] )->find();
			if( empty($res) == true ){
				\Db::table( $this->name )->insert([
					'user_guid' => get_uuid(),
				 	'user_name' => $vipInfo['shop_name'],
				 	'user_code' => $vipInfo['phone'],
				 	'shop_id' => $vipInfo['shop_id'],
				 	'uid' => $vipInfo['uid'],
				 	'org_id'  => 2,
				 	'is_admin' => 0
				]);
			}else{
				\Db::table( $this->name )->where('user_code',$vipInfo['phone'])->update([
				 	'user_name' => $vipInfo['shop_name'],
				 	'shop_id' => $vipInfo['shop_id'],
				 	'uid' => $vipInfo['uid'],
				]);
			}
			$userInfo = \Db::table($this->name)->where('user_code',$vipInfo['phone'])->find();
		}else{
			$userInfo = \Db::table( $this->name )
				->where($this->login_code_field ,"$user_name")
				->where($this->login_pwd_field , md5('wei'.md5($user_pwd)) )
				->field($this->login_pwd_field ,true)
				->find();
		}
		
		if( empty($userInfo) == true ){
			return json(array("code"=>-1,"msg"=>"账号和密码不符"));
		}
		if( $userInfo['is_enable'] == "0" ){
			return json(array("code"=>-1,"msg"=>"该账户已被禁用"));
		}
		
		/*登录成功-更新对应信息*/
		$data['land_time'] = (new \DateTime() )->format('Y-m-d H:i:s');
		$data['land_ip'] = request()->ip();
		$data['land_status'] = 1;
		$data['only_no'] = md5( $token['data'] );
		if( ! \Db::table($this->name)->where($this->key,$userInfo[$this->key])->update($data) ){
			return json(array("code"=>-1,"msg"=> "更新最后登录时间失败！"  ));
		}
		
		/* 登录成功-记录登录 */
		/*获取tokeen-更新对应信息*/
		$token = Auths::instance('web')->expTime(86400 * 5)->login($userInfo);
	    Cookie::set('token',$token['access_token']);
        
		/* 返回 */
		return json(array("code"=>0,"data"=>$token['data'],"msg"=>"登录成功"));
    }
	
	 /**
     * 注销登录状态
     * @return boolean
     */
    public function logout($key='')
    {
    	Auths::instance('web')->logout( Cookie::get('token') );
        return json(array("code"=>0,"data"=>"","msg"=>"退出成功"));
    }
	
    /**
     * 检验用户是否已经登陆
     * @return boolean 失败返回false，成功返回当前登陆用户基本信息
     */
    public function getUserInfo()
    {
    	$token = Cookie::get('token');
		if( !$user = Auths::instance('web')->getTokenInfo($token) ){
			return false;
		}
    	$key = $user['jwt']['info']["$this->key"];
		return \Db::table($this->name)->where("$this->key","$key")->find();	
    }

}
